package com.byb.server.service.impl;

import com.byb.server.service.SexService;
import com.byb.server.domain.Sex;
import com.byb.server.repository.SexRepository;
import com.byb.server.repository.search.SexSearchRepository;
import com.byb.server.service.dto.SexDTO;
import com.byb.server.service.mapper.SexMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Sex.
 */
@Service
@Transactional
public class SexServiceImpl implements SexService{

    private final Logger log = LoggerFactory.getLogger(SexServiceImpl.class);

    private final SexRepository sexRepository;

    private final SexMapper sexMapper;

    private final SexSearchRepository sexSearchRepository;
    public SexServiceImpl(SexRepository sexRepository, SexMapper sexMapper, SexSearchRepository sexSearchRepository) {
        this.sexRepository = sexRepository;
        this.sexMapper = sexMapper;
        this.sexSearchRepository = sexSearchRepository;
    }

    /**
     * Save a sex.
     *
     * @param sexDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public SexDTO save(SexDTO sexDTO) {
        log.debug("Request to save Sex : {}", sexDTO);
        Sex sex = sexMapper.toEntity(sexDTO);
        sex = sexRepository.save(sex);
        SexDTO result = sexMapper.toDto(sex);
        //sexSearchRepository.save(sex);
        return result;
    }

    /**
     *  Get all the sexes.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<SexDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Sexes");
        return sexRepository.findAll(pageable)
            .map(sexMapper::toDto);
    }

    /**
     *  Get one sex by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public SexDTO findOne(Long id) {
        log.debug("Request to get Sex : {}", id);
        Sex sex = sexRepository.findOne(id);
        return sexMapper.toDto(sex);
    }

    /**
     *  Delete the  sex by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Sex : {}", id);
        sexRepository.delete(id);
        //sexSearchRepository.delete(id);
    }

    /**
     * Search for the sex corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<SexDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Sexes for query {}", query);
        Page<Sex> result = sexSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(sexMapper::toDto);
    }
}
